﻿using Modbus.Device;
using ModbusHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleDemo
{
    class Program
    {
        static DITag t,t1;
        static int i, j;
        //
        static void Main(string[] args)
        {

            
            EthernetConnector ec1 = new EthernetConnector("127.0.0.1", 502, EthernetMode.TCP);
            
            ModbusDeviceManager.Instance.SurveyCompleted += Instance_RaiseTransactionCompletedEvent;

            t = ModbusDeviceManager.Instance.AddTag<DITag>(ec1, 1, 1);
            t.TagChanged += a_RaiseTagChangedEvent;
            t1 = ModbusDeviceManager.Instance.AddTag<DITag>(ec1, 1, 2);
            t1.TagChanged += a_RaiseTagChangedEvent1;

            Console.ReadLine();
        }

        static void Instance_RaiseTransactionCompletedEvent(object sender, EventArgs e)
        {
            Console.WriteLine("Completed");
        }

        private static void a_RaiseTagChangedEvent(object sender, TagChangedEventArgs e)
        {
            Console.WriteLine("tag_0 " + t.Value+" "+e.Quality+" "+i++);
        }

        private static void a_RaiseTagChangedEvent1(object sender, TagChangedEventArgs e)
        {
            Console.WriteLine("tag_1 " + t.Value + " " + e.Quality + " " + j++);
        }


        static ModbusDevice SetModbusDevice()
        {
            TcpClient client = new TcpClient("127.0.0.1", 502);
            return ModbusIpMaster.CreateIp(client);
        }

        static void resetModbusDevice()
        {

        }
    }

}
